package com.walter;

import cn.hutool.json.JSONObject;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * @author ：ch
 * @date ：Created in 2019/5/21
 * @description：
 * @modified By：
 * @version: 1.0
 */
public class AuthUser implements UserDetails {

    private static final long serialVersionUID = -2336372258701871345L;

    //用户
    private JSONObject user;

    public AuthUser(JSONObject user) {
        this.setUser(user);
    }

    // 角色权限 spring-security todo
	/*public static Collection<? extends GrantedAuthority> getAuthoritiesByRole(String role) {
		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();

		List<String> roles = Arrays.asList(role.split(","));
		if (roles.contains("user")) {
			authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
		}
		if (roles.contains("admin")) {
			authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		}

		return authorities;
	}*/

    // 提供权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
        // 角色权限 spring-security todo
//		return getAuthoritiesByRole(getUser().getRole());
    }

    // 提供账号名称
    @Override
    public String getUsername() {
        return getUser().getStr("userName");
    }

    // 提供密码
    @Override
    public String getPassword() {
        return getUser().getStr("password");
    }

    // 账号是否没过期，过期的用户无法认证
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账号是否没锁住，锁住的用户无法认证
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 密码是否没过期，密码过期的用户无法认证
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 用户是否使能，未使能的用户无法认证
    @Override
    public boolean isEnabled() {
        return true;
    }

    public JSONObject getUser() {
        return user;
    }

    public void setUser(JSONObject user) {
        this.user = user;
    }

}
